﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FlexLib.FileTree
{
	public class FileTreeNode
	{

		private List<FileTreeNode> subNodes = new List<FileTreeNode>();
		public List<FileTreeNode> SubNodes
		{
			get	{ return subNodes; }
		}

		public void AddNode(FileTreeNode node)
		{
			subNodes.Add(node);
			node.parent = this;
		}

		public FileTreeNode AddNode(string name)
		{
			FileTreeNode node = new FileTreeNode(name);
			AddNode(node);
			return node;
		}

		public void RemoveNode(FileTreeNode node)
		{
			subNodes.Remove(node);
		}

		public void Remove()
		{
			if (this.parent != null)
				this.parent.RemoveNode(this);
		}

		public void ClearNodes()
		{
			if (subNodes.Count == 0)
				return;
			subNodes.Clear();
		}

		public void Clear()
		{
			ClearNodes();
			ClearFiles();
		}

		private List<string> files = new List<string>();
		public List<string> Files
		{
			get	{ return files; }
		}

		public void AddFile(string name)
		{
			files.Add(name);
		}

		public void RemoveFile(string name)
		{
			files.Remove(name);
		}

		public void ClearFiles()
		{
			if (files.Count == 0)
				return;
			files.Clear();
		}

		private FileTreeNode parent = null;
		public FileTreeNode Parent
		{
			get { return parent; }
		}

		public string GetNonRootedPath()
		{
			string path = "";
			if (parent != null && parent.parent != null)
			{
				path = parent.GetNonRootedPath() + System.IO.Path.DirectorySeparatorChar;
			}

			return path + this.name;
		}

		public string GetFullPath()
		{
			string path = "";
			if (parent != null)
			{
				path = parent.GetFullPath() + System.IO.Path.DirectorySeparatorChar;
			}

			return path + this.name;
		}

		private string name;
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		public FileTreeNode(string name)
		{
			this.name = name;
		}

		public bool HasSubNodes
		{
			get
			{
				if (SubNodes.Count > 0)
					return true;
				else
					return false;
			}
		}

	}
}
